﻿using HalconDotNet;
using Halcon二次开发.Model.储存每个Controller的单例对象;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Halcon二次开发.Model;
using Halcon二次开发.Model.自定义控件;
using Halcon二次开发.Controller.数据加载与保存;
using Halcon二次开发.Model.储放所有单例.储存每个单例界面;
using Halcon二次开发.Controller.多模板匹配;
using Halcon二次开发.View.相机;

namespace Halcon二次开发.View.多模板匹配
{
    public partial class H_PMA_S_AlignToolForm : Form
    {
        //工程文件路径
        public string filePath;
        //保存Json文件的路径
        public string jsonPath;
        //基准数据保存路径
        public string standardPath;
        //基准线的数据保存
        public string linestandarPath;
        //将自定义界面进行存储
        public List<UserControl> listNodes=new List<UserControl>();
        /// <summary>
        /// 每new一次就是相当于一个界面
        /// </summary>
        public Shap_S_Matching controller;

        public HwindowFlowNodeUrseControl hwindowFlowNode;
        //初始化
        public H_PMA_S_AlignToolForm(HwindowFlowNodeUrseControl hwindowFlowNode)
        {
            InitializeComponent();
            this.hwindowFlowNode = hwindowFlowNode;
            toolStripComboBox1.SelectedIndex = 0;

            numericUpDown3.Value = 45;
            numericUpDown4.Value = -45;
            //最小对比度
            numericUpDown2.Value = 30;
            comboBox1.SelectedIndex = 2;
            //矩形
            node1 = new Rectangle2Control();
            node1.Dock = DockStyle.Fill;
            node1.XUpDown.ValueChanged += Click;
            node1.YUpDown.ValueChanged += Click;
            node1.Length1UpDown.ValueChanged += Click;
            node1.Length2UpDown.ValueChanged += Click;
            node1.PhiUpDown.ValueChanged += Click;
            //圆形
            node2 = new CirleControl();
            node2.Dock = DockStyle.Fill;
            node2.XUpDown.ValueChanged += Click;
            node2.YUpDown.ValueChanged += Click;
            node2.RaduisUpDown.ValueChanged += Click;
            //将node进行存储
            listNodes.Add(node1);
            listNodes.Add(node2);
            //默认显示矩形
            groupBox3.Controls.Add(node1);
            //默认选择第2个关闭
            comboBox7.SelectedIndex = 1;
            controller = new Shap_S_Matching(this,node1,node2,hwindowFlowNode);
            comboBox2.SelectedIndex = 0;
            comboBox3.SelectedIndex = 0;
            comboBox4.SelectedIndex = 0;
            comboBox5.SelectedIndex = 0;
            this.MinimizeBox = false;
        }

        public void SetPathNodes(string filePath, string nodes)
        {
            this.linestandarPath = filePath;
            this.jsonPath = filePath + "\\" + "界面数据" + "\\" + nodes + "\\" + nodes + "数据.json";
            this.filePath = (filePath + "\\" + "模板匹配文件" + "\\" + nodes + "工位模板.shm");
            this.standardPath = filePath + "\\" + "基准数据" + "\\" + nodes + "基准数据";
            //显示数据PMA
            ShowData();
        }
        /// <summary>
        /// 将保存之后的数据进行展示到界面上
        /// </summary>
        public void ShowData()
        {
            //调用存储方法
            new LoadData().LoadDatas(jsonPath,this, listNodes);
        }
        /// <summary>
        /// 画矩形
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            //点击绘制时获取模板匹配数据
            foreach (TreeNode item in hwindowFlowNode.treeView1.Nodes)
            {
                if (item.Name.Contains("相机"))
                {
                    controller.HideIntputImage = (item.Tag as CamerForm).controller.HideOutputImage;
                }
            }

            if (controller.HideIntputImage == null)
            {
                (FormDb.Db["Form1"] as Form1).textBox1.AppendText(this.Name+"当前图像为空!\r\n");
                return;
            }
            if (toolStripComboBox1.SelectedItem.ToString() == "旋转矩形")
            {
                HOperatorSet.ClearWindow(hwindowFlowNode.hSmartWindowControl1.HalconWindow);
                HOperatorSet.DispObj(controller.HideIntputImage,hwindowFlowNode.hSmartWindowControl1.HalconWindow);
                controller.DrawRectangle2();

            } else if (toolStripComboBox1.SelectedItem.ToString() == "圆形") {
                HOperatorSet.ClearWindow(hwindowFlowNode.hSmartWindowControl1.HalconWindow);
                HOperatorSet.DispObj(controller.HideIntputImage, hwindowFlowNode.hSmartWindowControl1.HalconWindow);
                controller.DrawCirle();
            }
        }

        private void H_PMAAlignToolForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.Hide();
            e.Cancel = true;
            controller.ClearTwoObject(hwindowFlowNode.hSmartWindowControl1.HalconWindow);
        }
        ///// <summary>
        ///// 搜索模板
        ///// </summary>
        ///// <param name="sender"></param>
        ///// <param name="e"></param>
        //private void toolStripButton3_Click(object sender, EventArgs e)
        //{

        //}
        /// <summary>
        /// 创建模板
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            try
            {
                controller.CreatShapeModel();
            }
            catch(Exception ex)
            {
                (FormDb.Db["Form1"] as Form1).textBox1.AppendText(this.Name + ":" + ex.Message + "\r\n");
            }

        }
        /// <summary>
        /// 保存模板
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripButton5_Click(object sender, EventArgs e)
        {
            try
            {
                controller.SaveShapeModel(filePath);
            }
            catch(Exception ex)
            {
                (FormDb.Db["Form1"] as Form1).textBox1.AppendText(this.Name + ":" + ex.Message + "\r\n");
            }
        }
        /// <summary>
        /// 矩形选择改变
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        //矩形参数
        public Rectangle2Control node1;
        //圆形参数
        public CirleControl node2;
        private void toolStripComboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            groupBox3.Controls.Clear();
            string str = (sender as ToolStripComboBox).Text;
            if (str == "旋转矩形")
            {
                groupBox3.Controls.Add(node1);
            }
            else if (str == "圆形")
            {
                groupBox3.Controls.Add(node2);
            }
        }
        private void Click(object sender, EventArgs e)
        {
            //controller.Move(sender);
        }
        /// <summary>
        /// 单次运行
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            try
            {
                controller.Tool_Run();
            }
            catch(Exception ex)
            {
                (FormDb.Db["Form1"] as Form1).textBox1.AppendText(this.Name + ":" + ex.Message + "\r\n");
            }
        }
        private void H_PMA_S_AlignToolForm_Load(object sender, EventArgs e)
        {
            
        }
        /// <summary>
        /// 反射调用该方法更新窗体句柄
        /// </summary>
        /// <param name="hwindowFlowNode"></param>
        public void PushHWindon(HwindowFlowNodeUrseControl hwindowFlowNode)
        {
            this.hwindowFlowNode = hwindowFlowNode;
            controller.hwindowFlowNode = hwindowFlowNode;
            //\\{hwindowFlowNode.projectName}
            SetPathNodes($@"Data\工程文件\" + hwindowFlowNode.projectName+"\\"
            +hwindowFlowNode.name + "任务文件", StaicResouce.CopyTreeNode.Text);
        }
        //绘制搜索区域功能开启与关闭
        private void comboBox7_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox7.SelectedItem.Equals("开启"))
            {
                button6.Enabled = true;
                numericUpDown21.Enabled = true;
                numericUpDown22.Enabled = true;
                numericUpDown24.Enabled = true;
                numericUpDown23.Enabled = true;
                numericUpDown25.Enabled = true;
                Office_X.Enabled = true;
                Office_Y.Enabled = true;
                fontSize.Enabled = true;
                button2.Enabled = true;
                //comboBox2.SelectedIndex = 0;
                comboBox2.Enabled = true;
                comboBox4.Enabled = true;
                comboBox5.Enabled = true;
                controller.Open_Search();
            }
            else if (comboBox7.SelectedItem.Equals("关闭"))
            {
                button6.Enabled = false;
                numericUpDown21.Enabled = false;
                numericUpDown22.Enabled = false;
                numericUpDown24.Enabled = false;
                numericUpDown23.Enabled = false;
                numericUpDown25.Enabled = false;
                Office_X.Enabled = false;
                Office_Y.Enabled = false;
                fontSize.Enabled = false;
                button2.Enabled = false;
                //comboBox2.SelectedIndex = 1;
                comboBox2.Enabled = false;
                comboBox4.Enabled = false;
                comboBox5.Enabled = false;
                if (controller==null)
                {
                    return;
                }
                controller.Colse_Search();
            }
        }
        //绘制搜索区域
        private void button6_Click(object sender, EventArgs e)
        {
            controller.DrawSearch();
        }
        /// <summary>
        /// 涂抹不感兴趣的区域
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripButton6_Click(object sender, EventArgs e)
        {
            try
            {
                controller.Seamer();
            }
            catch (Exception ex){
                (FormDb.Db["Form1"] as Form1).textBox1.AppendText(this.Name + ":" + ex.Message + "\r\n");
            }
        }
        /// <summary>
        /// 确认搜索区域
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            controller.Put_Search();    
        }

        private void Office_Y_ValueChanged(object sender, EventArgs e)
        {
            controller.Move_Search();
        }
        public string BorderColor = "", FontColor="";
        private void comboBox4_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox4.SelectedIndex == 0)
            {
                BorderColor = "blue";
            }
            else if (comboBox4.SelectedIndex == 1)
            {
                BorderColor = "red";
            }
            else if (comboBox4.SelectedIndex == 2)
            {
                BorderColor = "green";
            }
            else if (comboBox4.SelectedIndex == 3)
            {
                BorderColor = "black";
            }
            else if (comboBox4.SelectedIndex == 4) {
                BorderColor = "orange";
            }
        }
        /// <summary>
        /// 模板匹配的精度
        /// </summary>
        public string PMA_SubPixel = "";
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox1.SelectedIndex == 0)
            {
                PMA_SubPixel = "none";
            }
            else if (comboBox1.SelectedIndex == 1)
            {
                PMA_SubPixel = "interpolation";
            }
            else if (comboBox1.SelectedIndex == 2)
            {
                PMA_SubPixel = "least_squares";
            }
            else if (comboBox1.SelectedIndex == 3)
            {
                PMA_SubPixel = "least_squares_high";
            }
            else if (comboBox1.SelectedIndex == 4)
            {
                PMA_SubPixel = "least_squares_very_high";
            }
        }

        private void comboBox5_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox5.SelectedIndex == 0)
            {
                FontColor = "blue";
            }
            else if (comboBox5.SelectedIndex == 1)
            {
                FontColor = "red";
            }
            else if (comboBox5.SelectedIndex == 2)
            {
                FontColor = "green";
            }
            else if (comboBox5.SelectedIndex == 3)
            {
                FontColor = "black";
            }
            else if (comboBox5.SelectedIndex == 4)
            {
                FontColor = "orange";
            }
        }
    }
}
